package com.example.ortools.start.cp;

import com.google.ortools.Loader;
import com.google.ortools.sat.*;

public class Cp公式最大值new {

    public static void main(String[] args) {

        /**
         * Maximize 2x + 2y + 3z subject to the following constraints:
         * x + 7⁄2 y + 3⁄2 z	≤	25
         * 3x - 5y + 7z	≤	45
         * 5x + 2y - 6z	≤	37
         * x, y, z	≥	0
         * x, y, z integers
         */


        Loader.loadNativeLibraries();


        CpModel model = new CpModel();
        IntVar a = model.newIntVar(0, 2, "a");
        IntVar b = model.newIntVar(0, 2, "b");
        IntVar c = model.newIntVar(0, 2, "c");
        IntVar d = model.newIntVar(0, 3, "d");


        model.addEquality(LinearExpr.weightedSum(new LinearArgument[]{a},new long[]{2}),LinearExpr.weightedSum(new LinearArgument[]{b},new long[]{1}));
        model.addEquality(LinearExpr.weightedSum(new LinearArgument[]{c},new long[]{1}),LinearExpr.weightedSum(new LinearArgument[]{d},new long[]{1}));
        model.addLessOrEqual(LinearExpr.weightedSum(new LinearArgument[]{b,c},new long[]{1,1}),2);

        model.maximize(LinearExpr.weightedSum(new LinearArgument[]{a,b,c,d},new long[]{10,20,20,10}));

        CpSolver solver = new CpSolver();
        CpSolverStatus status = solver.solve(model);

        if (status == CpSolverStatus.OPTIMAL || status == CpSolverStatus.FEASIBLE) {
            System.out.printf("Maximum of objective function: %f%n", solver.objectiveValue());
            System.out.println("a = " + solver.value(a));
            System.out.println("b = " + solver.value(b));
            System.out.println("c = " + solver.value(c));
            System.out.println("d = " + solver.value(d));

            System.out.println("Problem solved in " + solver.wallTime() + " milliseconds");
        } else {
            System.out.println("No solution found.");
        }

    }



}
